package com.lswms.modules.lswms.controller.check;

import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletResponse;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.alibaba.druid.util.StringUtils;
import com.lswms.common.annotation.LogOperation;
import com.lswms.common.constant.Constant;
import com.lswms.common.page.PageData;
import com.lswms.common.utils.ExcelUtils;
import com.lswms.common.utils.Result;
import com.lswms.common.validator.AssertUtils;
import com.lswms.common.validator.ValidatorUtils;
import com.lswms.common.validator.group.AddGroup;
import com.lswms.common.validator.group.DefaultGroup;
import com.lswms.common.validator.group.UpdateGroup;
import com.lswms.modules.lswms.MEGConstant;
import com.lswms.modules.lswms.dto.check.CheckdetailDTO;
import com.lswms.modules.lswms.entity.check.CheckdetailEntity;
import com.lswms.modules.lswms.excel.check.CheckdetailExcel;
import com.lswms.modules.lswms.service.check.CheckdetailService;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import springfox.documentation.annotations.ApiIgnore;

/**
 * 盘点明细
 *
 * @author DX
 * @since 1.0.0 2021-07-08
 */
@RestController
@RequestMapping("lswms/checkdetail")
@Api(tags = "盘点明细")
public class CheckdetailController {
	@Autowired
	private CheckdetailService checkdetailService;

	@GetMapping("page")
	@ApiOperation("分页")
	@ApiImplicitParams({
			@ApiImplicitParam(name = Constant.PAGE, value = "当前页码，从1开始", paramType = "query", required = true, dataType = "int"),
			@ApiImplicitParam(name = Constant.LIMIT, value = "每页显示记录数", paramType = "query", required = true, dataType = "int"),
			@ApiImplicitParam(name = Constant.ORDER_FIELD, value = "排序字段", paramType = "query", dataType = "String"),
			@ApiImplicitParam(name = Constant.ORDER, value = "排序方式，可选值(asc、desc)", paramType = "query", dataType = "String") })
//	@RequiresPermissions("lswms:checkdetail:page")
	public Result<PageData<CheckdetailEntity>> page(@ApiIgnore @RequestParam Map<String, Object> params) {
		PageData<CheckdetailEntity> page = checkdetailService.pageBySql(params);

		return new Result<PageData<CheckdetailEntity>>().ok(page);
	}

	@GetMapping("{id}")
	@ApiOperation("信息")
//	@RequiresPermissions("lswms:checkdetail:info")
	public Result<CheckdetailDTO> get(@PathVariable("id") Long id) {
		CheckdetailDTO data = checkdetailService.get(id);

		return new Result<CheckdetailDTO>().ok(data);
	}

	@PostMapping
	@ApiOperation("保存")
	@LogOperation("保存")
	@RequiresPermissions("lswms:check:save")
	public Result save(@RequestBody CheckdetailDTO dto) {
		// 效验数据
		ValidatorUtils.validateEntity(dto, AddGroup.class, DefaultGroup.class);

		checkdetailService.save(dto);

		return new Result();
	}

	@PutMapping
	@ApiOperation("修改")
	@LogOperation("修改")
	@RequiresPermissions("lswms:check:update")
	public Result update(@RequestBody CheckdetailDTO dto) {
		// 效验数据
		ValidatorUtils.validateEntity(dto, UpdateGroup.class, DefaultGroup.class);

		checkdetailService.update(dto);

		return new Result();
	}

	@DeleteMapping
	@ApiOperation("删除")
	@LogOperation("删除")
	@RequiresPermissions("lswms:check:delete")
	public Result delete(@RequestBody Long[] ids) {
		// 效验数据
		try {
			AssertUtils.isArrayEmpty(ids, "id");
			String msg = checkdetailService.valideAndDelete(ids);

			if (StringUtils.isEmpty(msg)) {
				return new Result();
			} else {
				return new Result().error(msg);
			}
		} catch (Exception e) {
			e.printStackTrace();
			return new Result().error(MEGConstant.IS_ERROR);
		}
	}

	@GetMapping("export")
	@ApiOperation("导出")
	@LogOperation("导出")
	@RequiresPermissions("lswms:check:export")
	public void export(@ApiIgnore @RequestParam Map<String, Object> params, HttpServletResponse response)
			throws Exception {
		List<CheckdetailEntity> list = checkdetailService.listBySql(params);

		ExcelUtils.exportExcelToTarget(response, null, list, CheckdetailExcel.class);
	}

	@PutMapping("editUpdate")
	@ApiOperation("修改")
	@LogOperation("修改")
	@RequiresPermissions("lswms:checkdetail:update")
	public Result editUpdate(@RequestBody List<CheckdetailDTO> dtolist) {
		// 效验数据
		// ValidatorUtils.validateEntity(dto, UpdateGroup.class, DefaultGroup.class);

		checkdetailService.editUpdate(dtolist);

		return new Result();
	}

	@GetMapping("getCheckdetail/{tray}")
	@ApiOperation("信息")
	public Result getCheckdetail(@PathVariable("tray") String tray) {
		List<CheckdetailEntity> data = checkdetailService.getByTray(tray);

		return new Result().ok(data);
	}

	@GetMapping("getcheckdetailKeyAndLocationByTray/{tray}")
	@ApiOperation("信息")
	public Result getcheckdetailKeyAndLocationByTray(@PathVariable("tray") String tray) {
	//	List<CheckdetailEntity> data = checkdetailService.getByTray(tray);
		
		//获取当前的盘点号
		Map<String,Object> map = checkdetailService.getcheckdetailKeyAndLocationByTray(tray);

		return new Result().ok(map);
	}
	
	@PostMapping("saveCheckdetailIn")
	@ApiOperation("保存")
	@LogOperation("保存")
//	@RequiresPermissions("lswms:checkdetail:save")
	public Result saveCheckdetailIn(@RequestBody CheckdetailEntity dto) {
		// 效验数据
		try {
			// 效验数据
			// AssertUtils.isArrayEmpty(receiptkey, "receiptkey");
			String msg = checkdetailService.saveCheckdetailIn(dto);

			if (StringUtils.isEmpty(msg)) {
				return new Result();
			} else {
				return new Result().error(msg);
			}
		} catch (Exception e) {
			e.printStackTrace();
			return new Result().error(MEGConstant.IS_ERROR);
		}
	}
	
	@PutMapping("putCheckdetailSave")
	@ApiOperation("保存")
	@LogOperation("保存")
//	@RequiresPermissions("lswms:checkdetail:save")
	public Result putCheckdetailSave(@RequestBody List<CheckdetailEntity> dtos) {
		// 效验数据
		try {
			// 效验数据
			// AssertUtils.isArrayEmpty(receiptkey, "receiptkey");
			String msg = checkdetailService.putCheckdetailSave(dtos);

			if (StringUtils.isEmpty(msg)) {
				return new Result();
			} else {
				return new Result().error(msg);
			}
		} catch (Exception e) {
			e.printStackTrace();
			return new Result().error(MEGConstant.IS_ERROR);
		}
	}

	@PutMapping("putCheckdetailSave2")
	@ApiOperation("保存")
	@LogOperation("保存")
//	@RequiresPermissions("lswms:checkdetail:save")
	public Result putCheckdetailSave2(@RequestBody List<CheckdetailEntity> dtos) {
		//保存不回库
		// 效验数据
		try {
			String msg = checkdetailService.putCheckdetailSave2(dtos);

			if (StringUtils.isEmpty(msg)) {
				return new Result();
			} else {
				return new Result().error(msg);
			}
		} catch (Exception e) {
			e.printStackTrace();
			return new Result().error(MEGConstant.IS_ERROR);
		}
	}
	
	@PutMapping("putSaveUniquecodes")
	@ApiOperation("保存")
	@LogOperation("保存")
//	@RequiresPermissions("lswms:checkdetail:save")
	public Result putSaveUniquecodes(@RequestBody CheckdetailEntity dto) {
		// 效验数据
		try {
			// 效验数据
			// AssertUtils.isArrayEmpty(receiptkey, "receiptkey");
			String msg = checkdetailService.putCheckdetailSaveUniquecodes(dto);

			if (StringUtils.isEmpty(msg)) {
				return new Result();
			} else {
				return new Result().error(msg);
			}
		} catch (Exception e) {
			e.printStackTrace();
			return new Result().error(MEGConstant.IS_ERROR);
		}
	}
	
}